package ca.jsbr.as2js_lib.display.canvas.view.input 
{
	import ca.jsbr.as2js_lib.display.canvas.view.Image;
	import ca.jsbr.as2js_lib.event.MouseJSEvent;
	import ca.jsbr.as2js_lib.utils.enum.Cursor;
	import ca.jsbr.as2js_lib.utils.geom.Rect;
	import ca.jsbr.as2js_lib.utils.MouseManager;
	import js.element.canvas.CanvasRenderingContext2D;
	import js.event.Event;
	/**
	 * ...
	 * @author ...
	 */
	public class ButtonView extends Image 
	{
		static public const UP:int = 0;
		static public const OVER:int = 1;
		static public const DOWN:int = 2;
		static public const DISABLE:int = 3;
		
		private var _stateNum:int;
		
		public function ButtonView(src:*=null,stateNum:int=4) 
		{
			super(src);
			_stateNum = stateNum;
			
		}
		

		
		override protected function onLoad(e:Event):void 
		{
			super.onLoad(e);
			addEventListener(MouseJSEvent.MOUSE_OVER, onRollOver);
			addEventListener(MouseJSEvent.MOUSE_OUT, onRollOut);
			addEventListener(MouseJSEvent.MOUSE_DOWN, onMouseDown);
			addEventListener(MouseJSEvent.MOUSE_UP, onMouseUp);
			_height
		}
		
		private function onMouseDown(e:MouseJSEvent):void 
		{
			if(_state!=DISABLE)
				setState(DOWN);
		}
		
		private function onMouseUp(e:MouseJSEvent):void 
		{
			if(_state!=DISABLE)
				setState(UP);
		}
		
		private function onRollOut(e:MouseJSEvent):void 
		{
			if (_state != DISABLE)
			{
				MouseManager.getInstance().cursor = Cursor.AUTO;
				setState(UP);
			}
		}
		
		private function onRollOver(e:MouseJSEvent):void 
		{
			if (_state != 4)
			{
				MouseManager.getInstance().cursor = Cursor.POINTER;
				setState(OVER);
			}
		}
		
		override public function get height():Number 
		{
			return _height/_stateNum;
		}
		
		override public function set height(value:Number):void 
		{
			_height = value*_stateNum;
		}
		

		public function setState(sate:int):void
		{
			_state = sate;
		}
		
		override public function render(ctx:CanvasRenderingContext2D):void 
		{
			if (_ready)
			{
				if (_bitmapData)
				{
					if (_bitmapData.isModified())
						_bitmapData.render();
					ctx.putImageData(_bitmapData.imageData, 0, _state * height, _width, height, stageX, stageY, _width, height);
				}
				else
					ctx.drawImage(_image, 0, _state* height, _width, height, stageX, stageY, _width, height);
			}
		}
		
	}

}